package com.qianniu.napi.admin.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.qianniu.napi.admin.entity.Order;
import com.qianniu.napi.admin.entity.OrderGoods;
import com.qianniu.napi.admin.mapper.OrderGoodsMapper;
import com.qianniu.napi.admin.mapper.OrderMapper;
import com.qianniu.napi.admin.service.IOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author chenxuebin
 * @since 2018-06-24
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    private static final Logger logger = LoggerFactory.getLogger(OrderServiceImpl.class);

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderGoodsMapper orderGoodsMapper;


    public List<OrderGoods> getOrderGoodsPage(String uid, int pageSize, int pageIndex, Integer status) {

        Map<String, Object> map = new HashMap<String, Object>();
        Wrapper<Order> entity = new EntityWrapper<Order>()
                .eq("uid", "uid").eq("state", 1);
        if (status != 99) {
            entity.eq("status", status);
            map.put("status", status);
        }
        int total = orderMapper.selectCount(entity);

        Page page = new Page(pageIndex, pageSize);

        page.setTotal(total);

        map.put("uid", uid);
        map.put("size", pageSize);
        map.put("skip", page.getOffset());

        List<OrderGoods> list = orderGoodsMapper.selectOrderGoodsList(map);
        return list;
    }

    public List<Order> getOrderPage(String uid, int pageSize, int pageIndex, int status) {

        Map<String, Object> map = new HashMap<String, Object>();
        Page<Order> page = new Page<Order>(pageIndex, pageSize);


        Wrapper<Order> entity = new EntityWrapper<Order>().eq("uid", "uid").eq("state", 1);
        if (status != 99) {
            entity.eq("status", status);
            map.put("status", status);
        }
        entity.orderBy("createtime", false);


        int total = orderMapper.selectCount(entity);

        page.setTotal(total);

        map.put("uid", uid);
        map.put("size", pageSize);
        map.put("skip", page.getOffset());

        List<Order> list = orderGoodsMapper.selectOrderList(map);
        return list;
    }

    public List<Order> getPTOrderGoodsPage(Long uid, int pageIndex, int pageSize, Integer status,
                                           Integer isrefund, Integer pstatus) {

        Page page = new Page(pageIndex, pageSize);
        Map<String, Object> map = new HashMap<String, Object>();
        if (uid != null) map.put("uid", uid);
        if (status != null) map.put("status", status);
        if (pstatus != null) map.put("pstatus", pstatus);
        if (isrefund != null) map.put("isrefund", isrefund);
        logger.info("----------------------------------map=" + JSON.toJSONString(map));

        int total = orderMapper.selectPTOrderCount(map);
        page.setTotal(total);

        logger.info("----------------------------------page.getOffset()=" + page.getOffset());
        logger.info("-----------------page.getLimit()=" + page.getLimit());

        map.put("size", pageSize);
        map.put("skip", page.getOffset());

        List<Order> list = orderMapper.selectPTOrderPage(map);

        return list;
    }
}
